<template>
  <!-- 导航部分 -->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>工装夹具管理</el-breadcrumb-item>
        <el-breadcrumb-item>类型设置</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

  <!--搜索栏-->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-form inline>
        <el-form-item label="类型编码">
          <el-input v-model="toolQuery.toolTypeCode"/>
        </el-form-item>
        <el-form-item label="类型名称">
          <el-input v-model="toolQuery.toolTypeName"/>
        </el-form-item>
        <el-form-item label="保养维护类型">
          <el-select style="width: 150px" v-model="toolQuery.maintainType">
            <el-option label="全部"/>
            <el-option value="定期维护" label="定期维护"/>
            <el-option value="按使用次数维护" label="按使用次数维护"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="loadToolType" type="primary">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

  <!-- 工具栏 -->
  <el-row style="margin: 20px">
    <el-col :span="24">
      <el-button type="primary" plain @click="toAdd"><el-icon><Plus /></el-icon>新增</el-button>
      <el-button type="success" plain @click="toUpdate"><el-icon><EditPen /></el-icon>编辑</el-button>
      <el-button type="warning" plain @click="toDeletes"><el-icon><EditPen /></el-icon>删除</el-button>
    </el-col>
  </el-row>

<!--  &lt;!&ndash; 工具栏 &ndash;&gt;-->
<!--  <el-row style="margin: 10px">-->
<!--    <el-col :span="24">-->
<!--      <el-button type="primary" @click="toAdd">新增</el-button>-->
<!--      <el-button type="primary" @click="toUpdate">编辑</el-button>-->
<!--      <el-button type="primary" @click="toDeletes">删除</el-button>-->
<!--    </el-col>-->
<!--  </el-row>-->

  <!-- 数据列表 -->
  <el-row style="margin: 0px 10px;padding: 0px 15px">
    <el-col :span="24">
      <el-table :data="tools" border stripe ref="toolTable">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="toolTypeCode" label="类型编码">
          <template #default="scope">
            <el-link @click="showDetail(scope.row.toolTypeId)" type="success">
              {{scope.row.toolTypeId}}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="toolTypeName" label="类型名称"/>
        <el-table-column prop="codeManage" label="是否编码管理">
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.codeManage == 1">是</el-tag>
            <el-tag type="warning" v-if="scope.row.codeManage == 2">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="maintainType" label="维护类型"/>
        <el-table-column prop="serviceInterval" label="保养周期"/>
        <el-table-column prop="toolTypeNotes" label="备注"/>
        <el-table-column label="操作" width="330">
          <template #default="scope">
            <el-link style="margin: 0px 5px" @click="toEdit(scope.row.toolTypeId)" type="primary">
              <el-icon>
                <EditPen/>
              </el-icon>
              编辑
            </el-link>
            <el-link style="margin: 0px 5px" @click="toDelete(scope.row.toolTypeId)" type="primary">
              <el-icon>
                <Delete/>
              </el-icon>
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>

  <el-row style="margin-top: 10px">
    <!-- 监听页码的变化事件 -->
    <el-pagination @change="changePage" :total="total" background layout="prev, pager, next"/>
  </el-row>

  <!-- 新增的弹窗 -->
  <el-dialog v-model="addToolTypeVisible"
             title="新增工装夹具类型"
             width="50%"
  >
    <el-form :model="newToolType">
      <el-form-item label="类型编码">
        <el-input v-model="newToolType.toolTypeCode"/>
      </el-form-item>
      <el-form-item label="类型名称">
        <el-input v-model="newToolType.toolTypeName"/>
      </el-form-item>
      <!--      <el-form-item label="计划类型">-->
      <!--        <el-input v-model="newPlan.planType"/>-->
      <!--      </el-form-item>-->
      <el-form-item label="是否编码管理">
        <el-radio-group v-model="newToolType.codeManage">
          <el-radio value="1" size="large">是</el-radio>
          <el-radio value="2" size="large">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="保养维护类型">
        <el-radio-group v-model="newToolType.maintainType">
          <el-radio value="定期维护" size="large">定期维护</el-radio>
          <el-radio value="按使用次数维护" size="large">按使用次数维护</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="保养周期">
        <el-input v-model="newToolType.serviceInterval"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="newToolType.toolTypeNotes"/>
      </el-form-item>

    </el-form>
    <el-button @click="addToolTypeVisible =false" newPlan={}>取消</el-button>
    <el-button type="primary" @click="addToolType">
      确定
    </el-button>
  </el-dialog>

  <!--编辑子组件-->
  <ToolEdit :toolTypeId="updateToolTypeId" @editComplate="loadToolType"/>

  <typeDetail :toolTypeId="detailToolTypeId"/>

</template>

<script setup>
import {onMounted, ref} from "vue";
import toolsTypeRequest from "@/api/toolsType.js";
import {ElMessage, ElMessageBox} from "element-plus";
import maintainPlanRequest from "@/api/maintainPlan.js";
import ToolEdit from "@/views/tools/toolTypeEdit.vue"
import typeDetail from "@/views/tools/toolTypeDetail.vue";
import {EditPen} from "@element-plus/icons-vue";
let toolQuery = ref({});
let tools = ref([]);
let curPage = ref(1);
let total = ref(10);
let toolTable = ref([]);
let addToolTypeVisible = ref(false);
let newToolType = ref({});
let updateToolTypeId = ref(-1);
let detailToolTypeId = ref(-1);
//详情
function showDetail(toolTypeId){
  detailToolTypeId.value=toolTypeId;
}
//工具栏删除
function toDeletes() {
  let selectionRows = toolTable.value.getSelectionRows();
  if (selectionRows.length == 0) {
    ElMessage({
      type: "warning",
      message: "请选择数据"
    })
    return;
  }
  ElMessageBox.confirm(
      '您确定要删除吗？',
      'warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
    selectionRows.forEach(row => {
      toolsTypeRequest.deleteToolTYpe(row.toolTypeId, res => {
        if (res.code == 1) {
          ElMessage({
            type: "success",
            message: "删除成功",
          })
        } else {
          ElMessage({
            type: "warning",
            message: "删除失败",
          })
        }
      })
    })
    loadToolType();
  })
}

//工具栏新增
function toAdd() {
  addToolTypeVisible.value = true;
}

//工具栏新增按钮
function addToolType() {
  toolsTypeRequest.addToolType(newToolType.value, res => {
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "添加成功"
      })
    } else {
      ElMessage({
        type: "warning",
        message: "添加失败"
      })
    }
    newToolType.value = {};
    addToolTypeVisible.value = false;
    loadToolType();
  })
}

//工具啦编辑
function toUpdate(){
  let selectionRows = toolTable.value.getSelectionRows();
  if (selectionRows.length > 1){
    ElMessage({
      type:"warning",
      message:"只能选择一条数据"
    })
  }else if(selectionRows.length == 0){
    ElMessage({
      type:"warning",
      message:"请选择数据"
    })
  }else{
    toEdit(selectionRows[0].toolTypeId);
  }
}

//行编辑
function toEdit(toolTypeId) {
  updateToolTypeId.value = toolTypeId;
}

//行删除
function toDelete(toolTypeId) {
  ElMessageBox.confirm(
      '您确定要删除吗',
      'Warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
    toolsTypeRequest.deleteToolTYpe(toolTypeId, res => {
      if (res.code == 1) {
        ElMessage({
          type: "success",
          message: "操作完成"

        })
        loadToolType();
      } else {
        ElMessage({
          type: "warning",
          message: "删除失败"
        })
      }
    })
    loadToolType();
  })
}

function loadToolType() {
  toolsTypeRequest.loadToolType(toolQuery.value, curPage.value, 10, res => {
    total.value = res.data.total;
    tools.value = res.data.list;
  })
}

function changePage(value) {
  curPage.value = value;
  loadToolType();
}

onMounted(() => {
  loadToolType();
})
</script>

<style scoped>

</style>
<!--<template>-->
<!--  <h2>123</h2>-->
<!--</template>-->